package com.zdsoft.blp.zongbuju.shouye.taoshihui.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.util.Log;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.PopupWindow;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.zdsoft.blp.R;
import com.zdsoft.blp.gongyong.API.API;
import com.zdsoft.blp.gongyong.fanhui.FanHuiView;
import com.zdsoft.blp.gongyong.net.BaseActivity;
import com.zdsoft.blp.gongyong.net.Net;
import com.zdsoft.blp.gongyong.net.NetCallback;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by pc on 2016/9/2.
 */
public class SHAddressActivity extends BaseActivity implements View.OnClickListener ,NetCallback{
    private FanHuiView fh ;
    private TextView tv_baocun,et_xxaddress,et_name,et_phone;
    private static final int ADDRESSSAVE =1;
    private CheckBox cb_moren;
    private boolean isFirst;
    private Spinner sp;
    private String jieDao ="";
    @Override
    protected void init() {
        setContentView(R.layout.tsh_address);
        fh = find(R.id.fh);
        fh.getTv_title().setText("收货地址");

        sp = find(R.id.sp);
        tv_baocun = find(R.id.tv_baocun);
        et_xxaddress = find(R.id.et_xxaddress);
        et_name = find(R.id.et_name);
        et_phone = find(R.id.et_phone);
        cb_moren = find(R.id.cb_moren);

        isFirst =getIntent().getBooleanExtra("isFirst",false);
        if(isFirst){
            cb_moren.setChecked(true);
            cb_moren.setEnabled(false);
        }
        final String[] mItems = getResources().getStringArray(R.array.qu);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mItems);
        adapter.setDropDownViewResource(android.R.layout.select_dialog_singlechoice);
        sp.setAdapter(adapter);
        sp.setPrompt("请选择城市");
        sp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                jieDao = mItems[position];
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

        tv_baocun.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tv_baocun:
                String xxaddress = et_xxaddress.getText().toString();
                String name = et_name.getText().toString();
                String phone = et_phone.getText().toString();
                if("".equals(jieDao)){
                    Toast.makeText(getApplicationContext(),"街道不能为空",Toast.LENGTH_SHORT).show();
                    break;
                }else if("".equals(xxaddress)){
                    Toast.makeText(getApplicationContext(),"详细地址不能为空",Toast.LENGTH_SHORT).show();
                    break;
                }else if("".equals(name)){
                    Toast.makeText(getApplicationContext(),"姓名不能为空",Toast.LENGTH_SHORT).show();
                    break;
                }else if("".equals(phone)){
                    Toast.makeText(getApplicationContext(),"电话不能为空",Toast.LENGTH_SHORT).show();
                    break;
                }else {
                    SharedPreferences sp = SHAddressActivity.this.getSharedPreferences("info", Context.MODE_PRIVATE);
                    String userId = sp.getString("userId", "");
                    Net net = new Net();
                    net.url = API.ADDRESS_URL;
                    net.nameSpace = API.ADDRESS_NAMESPACE;
                    net.methodName = API.ADDRESSSAVE;
                    Map<String, Object> map = new HashMap<>();
                    map.put("id", "");
                    map.put("xingming", name);
                    map.put("dianhua", phone);
                    map.put("guojia", "中国");
                    map.put("sheng", "河南省");
                    map.put("shi", "郑州市");
                    map.put("qu", jieDao);
                    map.put("xiangxi", xxaddress);
                    map.put("youbian", "473000");
                    if(isFirst){
                        map.put("moren", "Y");
                    }else {
                        if(cb_moren.isChecked()){
                            map.put("moren", "Y");
                        }else {
                            map.put("moren", "N");
                        }
                    }
                    map.put("userId", userId);
                    net.parms = map;
                    net.code = ADDRESSSAVE;
                    getServer(net, SHAddressActivity.this);
                }
                break;
        }
    }

    @Override
    public void onResponse(boolean flag, int code, String res) {
        if(flag&&code==ADDRESSSAVE){
            try {
                JSONObject object = new JSONObject(res);
                if(object.getBoolean("result")){
                    SHAddressActivity.this.finish();
                }else {
                    Toast.makeText(getApplicationContext(),object.getString("msg"),Toast.LENGTH_SHORT).show();
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }
}
